package com.wenda.homeschooedu.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.wenda.homeschooedu.biz.ContactorManager;

public class ContactorServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		PrintWriter out  = null;
		String result = "";
		if (req.getParameter("format").equals("json")) {
			if (req.getParameter("type").equals("get")) {
				resp.setContentType("text/html;charset=utf-8");
				out = resp.getWriter();
				int class_id = Integer.parseInt(req.getParameter("class_id"));
				
				
				//order = 1 表示获得家长的集合；order = 0  表示获得学生的集合
				int order = Integer.parseInt(req.getParameter("order"));
				Gson gson = new Gson();
				if(order == 0){
					result = gson.toJson(new ContactorManager().getStudentsByClassId(class_id));
				}
				else if(order == 1) {
					result = gson.toJson(new ContactorManager().getParentsByClassId(class_id));
				}
			
				out.print(result);
				out.flush();
				out.close();
				
				
				
			}
		}
		
		
		
		
		
			
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}
	
	
	
}
